﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HKB.Common;
using HKB.Data;
using HKB.Data.Bll;
using HKB.Web;
using HKB.Web.Shop.Member;

public partial class Member_Shop_BuyManageOrder : Member
{
    private int UserID = 0;
    private string wherestr = "";
    //private ArrayList al = new ArrayList();
    private IDbDataParameter[] param = null;
    private string beforeurl = "Shop_BuyManageOrder.aspx?1=1";
    private string linkHead = "";
    private Shop_Order _order = null;
    private string proTypeWhere = "";
    protected Shop_Order m_order
    {
        get
        {
            Shop_Order order;
            if (this._order == null)
            {
                this._order = new Shop_Order(this.conn);
                order = this._order;
            }
            else
            {
                order = this._order;
            }
            return order;
        }
    }
    //public string ProTypeWhere
    //{
    //    get
    //    {
    //        if (!string.IsNullOrEmpty(base.ShowProType))
    //        {
    //            string showProType = base.ShowProType;
    //            if (showProType != null)
    //            {
    //                if (showProType == "1")
    //                {
    //                    this.ltrProType.Text = Language.Get("Shop/Product/ProductType1") + "——";
    //                    goto IL_14A;
    //                }
    //                if (showProType == "2")
    //                {
    //                    this.ltrProType.Text = Language.Get("Shop/Product/ProductType2") + "——";
    //                    goto IL_14A;
    //                }
    //                if (showProType == "3")
    //                {
    //                    this.ltrProType.Text = Language.Get("Shop/Product/ProductType3") + "——";
    //                    goto IL_14A;
    //                }
    //                if (showProType == "4")
    //                {
    //                    this.ltrProType.Text = Language.Get("Shop/Product/ProductType4") + "——";
    //                    goto IL_14A;
    //                }
    //                if (showProType == "5")
    //                {
    //                    this.ltrProType.Text = Language.Get("Shop/Product/ProductType5") + "——";
    //                    this.proTypeWhere = " and TradeType=5 ";
    //                    goto IL_14A;
    //                }
    //            }
    //            this.proTypeWhere = " and TradeType<>5 ";
    //        IL_14A: ;
    //        }
    //        else
    //        {
    //            this.proTypeWhere = " and TradeType<>5 ";
    //        }
    //        return this.proTypeWhere;
    //    }
    //}

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!base.IsPostBack)
        {
            this.lblUserName.Text = base.user.UserName;
            DataTable dataTable = BLLInstance.BllShoppingCarts.GetList(" userID=" + base.user.UserID).Tables[0];
            this.lblShopCarNum.Text = dataTable.Rows.Count.ToString();
            this.Buyer_Order.Text = GetRemindMe();

            this.UserID = base.user.UserID;
                this.BindList(string.Empty);
        }
    }

    protected void BindList(string TelPhone)
    {
        this.QuestString();
        //if (this.al.Count > 0)
        //{
        //    this.param = new IDbDataParameter[this.al.Count];
        //    this.al.CopyTo(this.param, 0);
        //}
        DataTable buyOrder = this.m_order.GetBuyOrder(TelPhone, this.wherestr, base.user.UserID, this.param);
        if (buyOrder != null)
        {
            int num = 0;
            if (Fetch.Get("pageindex") != null)
            {
                num = Convert.ToInt32(Fetch.GetID("pageindex").ToString());
                if (num == 0)
                {
                    num = 1;
                }
            }
            int num2 = 3;
            int count = buyOrder.Rows.Count;
            DataTable pagedTable = CurrPageLinks.instance.GetPagedTable(buyOrder, num, num2);
            this.DataTablePage(pagedTable);
            this.ButonShowHide();
            string url = this.beforeurl + this.linkHead + "&pageindex";
            this.ltrPagePanel.Text = CurrPageLinks.instance.CurrPageLinkStr(num, num2, count, "条记录&nbsp;每页" + num2 + "条记录&nbsp;", url, true);
        }
    }

    protected void QuestString()
    {
        this.wherestr = "and ((TradeType=2 and DateDiff(s,a.UpdateTime,getdate())>=0 and AuctionEndPrice in(select max(AuctionEndPrice) from HLShop_Order as a,HLShop_OrderItem as b where a.OrderFormID=b.OrderFormID Group by b.ProductID)) or (TradeType=3 and Teameffect=1) or (TradeType<>2 and TradeType<>3)) ";
        //if (base.ShowProType == "5")
        //{
        //    this.beforeurl += "&showProType=5";
        //}
        //string text = Fetch.Get("OrderFormNum");
        //if (!string.IsNullOrEmpty(text))
        //{
        //    this.TxtOrderNum.Text = text;
        //}
        //else
        //{
        //    text = this.TxtOrderNum.Text.Trim();
        //}
        //if (!string.IsNullOrEmpty(text))
        //{
        //    this.wherestr = this.wherestr + "and a.OrderFormNum like '%" + text.Trim() + "%' ";
        //    this.linkHead = this.linkHead + "&OrderFormNum=" + text.Trim();
        //}
        //string text2 = Fetch.Get("OrderName");
        //if (!string.IsNullOrEmpty(text2))
        //{
        //    this.TxtOrderName.Text = text2;
        //}
        //else
        //{
        //    text2 = this.TxtOrderName.Text.Trim();
        //}
        //if (!string.IsNullOrEmpty(text2))
        //{
        //    this.wherestr = this.wherestr + "and a.OrderFormID in (Select OrderFormID From [HLShop_OrderItem] Where ProductName like '%" + text2.Trim() + "%') ";
        //    this.linkHead = this.linkHead + "&OrderName=" + text2.Trim();
        //}
        //string text3 = Fetch.Get("BeginDate");
        //if (!string.IsNullOrEmpty(text3))
        //{
        //    this.TxtBeginDate.Text = text3;
        //}
        //else
        //{
        //    text3 = this.TxtBeginDate.Text.Trim();
        //}
        //if (!string.IsNullOrEmpty(text3))
        //{
        //    if (!Text.IsDate(text3))
        //    {
        //        this.WriteErrMsg("", Language.Get("Common/DateTimeFormatErr"), "");
        //    }
        //    this.wherestr = this.wherestr + "and DateDiff(dd,UpdateTime, '" + text3 + "')<=0 ";
        //    this.linkHead = this.linkHead + "&BeginDate=" + text3;
        //}
        //string text4 = Fetch.Get("EndDate");
        //if (!string.IsNullOrEmpty(text4))
        //{
        //    this.TxtEndDate.Text = text4;
        //}
        //else
        //{
        //    text4 = this.TxtEndDate.Text.Trim();
        //}
        //if (!string.IsNullOrEmpty(text4))
        //{
        //    if (!Text.IsDate(text4))
        //    {
        //        this.WriteErrMsg("", Language.Get("Common/DateTimeFormatErr"), "");
        //    }
        //    if (text3 != string.Empty)
        //    {
        //        if (Convert.ToDateTime(text3) > Convert.ToDateTime(text4))
        //        {
        //            this.WriteErrMsg("", Language.Get("Common/DateTimeErr"), "");
        //        }
        //    }
        //    this.wherestr = this.wherestr + "and DateDiff(dd,UpdateTime, '" + text4 + "')>=0 ";
        //    this.linkHead = this.linkHead + "&EndDate=" + text4;
        //}
        //string text5 = Fetch.Get("BeginMoney");
        //if (!string.IsNullOrEmpty(text5))
        //{
        //    this.TxtBeginMoney.Text = text5;
        //}
        //else
        //{
        //    text5 = this.TxtBeginMoney.Text.Trim();
        //}
        //if (!string.IsNullOrEmpty(text5))
        //{
        //    if (!Text.IsNumber(text5.Trim()))
        //    {
        //        this.WriteErrMsg("", Language.Get("Common/NotNumber"), "");
        //    }
        //    this.wherestr = this.wherestr + "and MoneyTotal>=" + text5.Trim() + " ";
        //    this.linkHead = this.linkHead + "&BeginMoney=" + text5.Trim();
        //}
        //string text6 = Fetch.Get("EndMoney");
        //if (!string.IsNullOrEmpty(text6))
        //{
        //    this.TxtEndMoney.Text = text6;
        //}
        //else
        //{
        //    text6 = this.TxtEndMoney.Text.Trim();
        //}
        //if (!string.IsNullOrEmpty(text6))
        //{
        //    if (!Text.IsNumber(text6.Trim()))
        //    {
        //        this.WriteErrMsg("", Language.Get("Common/NotNumber"), "");
        //    }
        //    this.wherestr = this.wherestr + "and MoneyTotal<=" + text6.Trim() + " ";
        //    this.linkHead = this.linkHead + "&EndMoney=" + text6.Trim();
        //}
        //string text7 = Fetch.Get("OrderStaticSel");
        //if (!string.IsNullOrEmpty(text7))
        //{
        //    this.DroOrderStatic.ddlSelect(text7);
        //}
        //else
        //{
        //    text7 = this.DroOrderStatic.SelectedValue.Trim();
        //}
        //if (!string.IsNullOrEmpty(text7))
        //{
        //    string text8 = text7;
        //    if (text8 != null)
        //    {
        //        if (!(text8 == "0"))
        //        {
        //            if (!(text8 == "1"))
        //            {
        //                if (!(text8 == "2"))
        //                {
        //                    if (!(text8 == "4"))
        //                    {
        //                        if (!(text8 == "5"))
        //                        {
        //                            if (text8 == "6")
        //                            {
        //                                object obj = this.wherestr;
        //                                this.wherestr = string.Concat(new object[]
        //                                {
        //                                    obj,
        //                                    "and Status=2 and a.OrderFormNum in (select OrderFormNum from HLMall_Credit where ValuerID=",
        //                                    base.user.UserID,
        //                                    ") "
        //                                });
        //                            }
        //                        }
        //                        else
        //                        {
        //                            object obj = this.wherestr;
        //                            this.wherestr = string.Concat(new object[]
        //                            {
        //                                obj,
        //                                "and Status=2 and a.OrderFormNum not in (select OrderFormNum from HLMall_Credit where ValuerID=",
        //                                base.user.UserID,
        //                                ") "
        //                            });
        //                        }
        //                    }
        //                    else
        //                    {
        //                        this.wherestr += "and Status=4 ";
        //                    }
        //                }
        //                else
        //                {
        //                    this.wherestr += "and Status=2 ";
        //                }
        //            }
        //            else
        //            {
        //                this.wherestr += "and Status=1 ";
        //            }
        //        }
        //        else
        //        {
        //            this.wherestr += "and Status=0 ";
        //        }
        //    }
        //    this.linkHead = this.linkHead + "&OrderStaticSel=" + text7;
        //}
        //string text9 = Fetch.Get("PayStaticSel");
        //if (!string.IsNullOrEmpty(text9))
        //{
        //    this.DroPayStatic.ddlSelect(text9);
        //}
        //else
        //{
        //    text9 = this.DroPayStatic.SelectedValue.Trim();
        //}
        //if (!string.IsNullOrEmpty(text9))
        //{
        //    string text8 = text9;
        //    if (text8 != null)
        //    {
        //        if (!(text8 == "0"))
        //        {
        //            if (!(text8 == "1"))
        //            {
        //                if (text8 == "2")
        //                {
        //                    this.wherestr += "and PayStatus=2 ";
        //                }
        //            }
        //            else
        //            {
        //                this.wherestr += "and PayStatus=1 ";
        //            }
        //        }
        //        else
        //        {
        //            this.wherestr += "and PayStatus=0 ";
        //        }
        //    }
        //    this.linkHead = this.linkHead + "&PayStaticSel=" + text9;
        //}
        //string text10 = Fetch.Get("DeliverStatusSel");
        //if (!string.IsNullOrEmpty(text10))
        //{
        //    this.DropDeliverStatus.ddlSelect(text10);
        //}
        //else
        //{
        //    text10 = this.DropDeliverStatus.SelectedValue.Trim();
        //}
        //if (!string.IsNullOrEmpty(text10))
        //{
        //    string text8 = text10;
        //    if (text8 != null)
        //    {
        //        if (!(text8 == "0"))
        //        {
        //            if (!(text8 == "1"))
        //            {
        //                if (!(text8 == "2"))
        //                {
        //                    if (!(text8 == "-1"))
        //                    {
        //                        if (text8 == "-2")
        //                        {
        //                            this.wherestr += "and DeliverStatus=-2 ";
        //                        }
        //                    }
        //                    else
        //                    {
        //                        this.wherestr += "and DeliverStatus=-1 ";
        //                    }
        //                }
        //                else
        //                {
        //                    this.wherestr += "and DeliverStatus=2 ";
        //                }
        //            }
        //            else
        //            {
        //                this.wherestr += "and DeliverStatus=1 ";
        //            }
        //        }
        //        else
        //        {
        //            this.wherestr += "and DeliverStatus=0 ";
        //        }
        //    }
        //    this.linkHead = this.linkHead + "&DeliverStatusSel=" + text10;
        //}
        //string text11 = Fetch.Get("MomentSel");
        //if (!string.IsNullOrEmpty(text11))
        //{
        //    this.DropMoment.ddlSelect(text11);
        //}
        //else
        //{
        //    text11 = this.DropMoment.SelectedValue.Trim();
        //}
        //if (!string.IsNullOrEmpty(text11))
        //{
        //    string text8 = text11;
        //    if (text8 != null)
        //    {
        //        if (!(text8 == "1"))
        //        {
        //            if (!(text8 == "3"))
        //            {
        //                if (!(text8 == "10"))
        //                {
        //                    if (text8 == "30")
        //                    {
        //                        this.wherestr += "and DateDiff(MM,InputTime, getdate())=0 ";
        //                    }
        //                }
        //                else
        //                {
        //                    this.wherestr += "and DateDiff(dd,InputTime, getdate())<=10 ";
        //                }
        //            }
        //            else
        //            {
        //                this.wherestr += "and DateDiff(dd,InputTime, getdate())<=3 ";
        //            }
        //        }
        //        else
        //        {
        //            this.wherestr += "and DateDiff(dd,InputTime, getdate())=0 ";
        //        }
        //    }
        //    this.linkHead = this.linkHead + "&MomentSel=" + text11;
        //}
        if (this.wherestr != string.Empty)
        {
            this.wherestr = "and" + this.wherestr;
            this.wherestr = this.wherestr.Replace("andand", "");
        }
    }
    private void DataTablePage(DataTable dt)
    {
        if (dt != null)
        {
            this.rptOrderInfo.DataSource = dt;
            this.rptOrderInfo.DataBind();
            for (int i = 0; i < this.rptOrderInfo.Items.Count; i++)
            {
                HiddenField hiddenField = (HiddenField)this.rptOrderInfo.Items[i].FindControl("hfOrderFormID");
                HiddenField hiddenField2 = (HiddenField)this.rptOrderInfo.Items[i].FindControl("hfOrderFormNum");
                if (hiddenField != null)
                {
                    if (!string.IsNullOrEmpty(hiddenField.Value))
                    {
                        string sql = "select (select COUNT(HLShop_OrderItem.OrderFormID) from HLShop_OrderItem where HLShop_OrderItem.OrderFormID=item.OrderFormID) as ItemCount\r\n        ,o.Status,o.DeliverStatus,o.PayStatus,\r\n        product.ProductImageUrl,product.ProductSmallImageUrl,item.*  \r\n        from HLShop_Order as o,HLShop_OrderItem as item,HLShop_Product as product \r\n        where o.OrderFormID=item.OrderFormID and product.ProductID=item.ProductID and item.OrderFormID in (" + hiddenField.Value + ")";
                        DataTable dataTable = this.conn.GetDataTable(sql);
                        Repeater repeater = (Repeater)this.rptOrderInfo.Items[i].FindControl("rptOrderItem");
                        if (repeater != null)
                        {
                            repeater.DataSource = dataTable;
                            repeater.DataBind();
                            for (int j = 0; j < repeater.Items.Count; j++)
                            {
                                HiddenField hiddenField3 = (HiddenField)repeater.Items[j].FindControl("hfitemCount");
                                Panel panel = (Panel)repeater.Items[j].FindControl("pnlRowspan");
                                if (hiddenField3 != null)
                                {
                                    if (!string.IsNullOrEmpty(hiddenField3.Value))
                                    {
                                        if (j == 0)
                                        {
                                            if (panel != null)
                                            {
                                                panel.Visible = true;
                                            }
                                        }
                                        else
                                        {
                                            if (panel != null)
                                            {
                                                panel.Visible = false;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (panel != null)
                                        {
                                            panel.Visible = false;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    private void ButonShowHide()
    {
        for (int i = 0; i < this.rptOrderInfo.Items.Count; i++)
        {
            Repeater repeater = (Repeater)this.rptOrderInfo.Items[i].FindControl("rptOrderItem");
            HiddenField hiddenField = (HiddenField)this.rptOrderInfo.Items[i].FindControl("hfTradeTypeID");
            if (repeater != null)
            {
                for (int j = 0; j < repeater.Items.Count; j++)
                {
                    RepeaterItem repeaterItem = repeater.Items[j];
                    HiddenField hiddenField2 = (HiddenField)repeaterItem.FindControl("hfPoductID");
                    HiddenField hiddenField3 = (HiddenField)repeaterItem.FindControl("hfOldPrice");
                    HiddenField hiddenField4 = (HiddenField)repeaterItem.FindControl("hfNeedPresentExp");
                    Literal literal = (Literal)repeaterItem.FindControl("ltr_OldPrice");
                    Literal literal2 = (Literal)repeaterItem.FindControl("ltr_PriceOrScore");
                    if (hiddenField != null)
                    {
                        if (hiddenField.Value.Trim() == "5")
                        {
                            if (hiddenField4 != null)
                            {
                                if (literal2 != null)
                                {
                                    literal2.Text = "换购积分：" + hiddenField4.Value;
                                }
                            }
                        }
                    }
                    HiddenField hiddenField5 = (HiddenField)repeaterItem.FindControl("hfDeliverStatus");
                    Button button = (Button)repeaterItem.FindControl("btnDeliverReturn");
                    Button button2 = (Button)repeaterItem.FindControl("BtnCusReceive");
                    HiddenField hiddenField6 = (HiddenField)repeaterItem.FindControl("hfOrderStatus");
                    HiddenField hiddenField7 = (HiddenField)repeaterItem.FindControl("hfPayStatus");
                    Button button3 = (Button)repeaterItem.FindControl("btnPayStatus");
                    Label label = (Label)repeaterItem.FindControl("lblComment");
                    Button button4 = (Button)repeaterItem.FindControl("btnDelOrder");
                    if (hiddenField6 != null)
                    {
                        if (hiddenField6.Value.Trim() == "2")
                        {
                            if (label != null)
                            {
                                label.Visible = true;
                            }
                        }
                        else
                        {
                            if (label != null)
                            {
                                label.Visible = false;
                            }
                        }
                    }
                    if (hiddenField7 != null)
                    {
                        if (hiddenField7.Value.Trim() == "0")
                        {
                            button3.Visible = true;
                            if (button4 != null)
                            {
                                if (hiddenField5 != null)
                                {
                                    if (hiddenField5.Value.Trim() == "0")
                                    {
                                        button4.Visible = true;
                                    }
                                    else
                                    {
                                        button4.Visible = false;
                                    }
                                }
                            }
                        }
                        else
                        {
                            button3.Visible = false;
                            if (button4 != null)
                            {
                                button4.Visible = false;
                            }
                        }
                    }
                    if (hiddenField5 != null)
                    {
                        if (hiddenField5.Value.Trim() == "1")
                        {
                            if (button != null)
                            {
                            }
                            if (button2 != null)
                            {
                                button2.Visible = true;
                            }
                        }
                        else
                        {
                            if (hiddenField5.Value.Trim() == "2")
                            {
                                if (button != null)
                                {
                                    button.Visible = false;
                                }
                                if (button2 != null)
                                {
                                    button2.Visible = false;
                                }
                            }
                            else
                            {
                                if (button != null)
                                {
                                    button.Visible = false;
                                }
                                if (button2 != null)
                                {
                                    button2.Visible = false;
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    protected void Button_Command(object sender, CommandEventArgs e)
    {
        string commandName = e.CommandName;
        string value = e.CommandArgument.ToString();
        string comeUrl = "Shop_BuyManageOrder.aspx";
        if (base.ShowProType == "5")
        {
            comeUrl = "Shop_BuyManageOrder.aspx?showProType=5";
        }
        if (commandName == "DelOrder")
        {
            if (!string.IsNullOrEmpty(value))
            {
                bool flag = false;
                int num = Convert.ToInt32(value);
                if (this.JustDelOrderFormNum(num))
                {
                    this.conn.BeginTransaction();
                    try
                    {
                        string commandText = "Delete from HLJXC_StockInfoOrder where OrderID=" + num;
                        string commandText2 = "Delete from HLJXC_StockItemOrder where InfoOrderID in (select InfoOrderID from HLJXC_StockInfoOrder where OrderID=" + num + ")";
                        string commandText3 = "Delete From [HLShop_Order] Where OrderFormID=" + num;
                        string commandText4 = "Delete From [HLShop_OrderItem] Where OrderFormID=" + num;
                        this.conn.ExecuteTransaction(commandText2);
                        this.conn.ExecuteTransaction(commandText);
                        this.conn.ExecuteTransaction(commandText4);
                        this.conn.ExecuteTransaction(commandText3);
                    }
                    catch
                    {
                        this.conn.Rollback();
                        flag = false;
                    }
                    finally
                    {
                        this.conn.Commit();
                        flag = true;
                    }
                }
                if (flag)
                {
                    this.WriteSuccessMsg("", "删除成功！", comeUrl);
                }
            }
        }
        else
        {
            if (commandName == "CusReceiveOrder")
            {
                if (!string.IsNullOrEmpty(value))
                {
                    int num = Convert.ToInt32(value);
                    OrderAction orderAction = new OrderAction(this.conn);
                    if (orderAction.CusReceive(num))
                    {
                        this.WriteSuccessMsg("", Language.Get("Common/Success"), comeUrl);
                    }
                    else
                    {
                        this.WriteErrMsg("", Language.Get("Common/Fail", ""), "");
                    }
                }
            }
        }
    }
    public string StrCommentStatus(string _OrderNum)
    {
        string text = "";
        DataTable dataTable = this.conn.GetDataTable("select * from HLMall_Credit where OrderFormNum='" + _OrderNum + "'");
        DataRow[] array = dataTable.Select(" ValuerID=" + base.user.UserID);
        string result;
        if (dataTable.Rows.Count == 2)
        {
            text = "双方已互评";
            dataTable.Dispose();
            result = text;
        }
        else
        {
            if (dataTable.Rows.Count == 0)
            {
                text = "<input id='btnComment' type='button' onclick=\"javascript:window.location.href='CreditComment.aspx?OrderFormNum=" + _OrderNum + "&IsBuySaler=0'\" value='评价' />";
                dataTable.Dispose();
                result = text;
            }
            else
            {
                if (array.Length == 1 && dataTable.Rows.Count == 1)
                {
                    text = "已评价<br/>对方未评价";
                    dataTable.Dispose();
                    result = text;
                }
                else
                {
                    if (array.Length == 0 && dataTable.Rows.Count == 1)
                    {
                        text = "<input id='btnComment' type='button' onclick=\"javascript:window.location.href='CreditComment.aspx?OrderFormNum=" + _OrderNum + "&IsBuySaler=0'\" value='评价' /><br/>对方已评价";
                        dataTable.Dispose();
                        result = text;
                    }
                    else
                    {
                        result = text;
                    }
                }
            }
        }
        return result;
    }

    protected bool JustDelOrderFormNum(int OrderID)
    {
        DataSet dataSet = this.conn.GetDataSet("select * from HLShop_Order where OrderFormID=" + OrderID);
        bool result;
        if (dataSet == null || dataSet.Tables[0].Rows.Count <= 0 || dataSet.ToString() == "")
        {
            result = false;
        }
        else
        {
            string a = dataSet.Tables[0].Rows[0]["PayStatus"].ToString();
            string text = dataSet.Tables[0].Rows[0]["Status"].ToString();
            result = (a == "0");
        }
        return result;
    }
}